package com.example.dao.daoImple;

import com.example.dao.VideoDao;
import com.example.entity.Video;
import com.example.entity.vo.VideoListVo;
import com.example.entity.vo.VideoSuggestVo;
import com.example.util.JDBCUtil;

import java.util.List;

public class VideoDaoImpl implements VideoDao {

    //添加视频
    @Override
    public boolean addVideo(Video video) {
        String sql = "INSERT INTO tb_video (user_id,title,url,create_time)  VALUES (?,?,?,?)";
        return JDBCUtil.ALLCUD(sql,video.getUserId(),video.getTitle(),video.getUrl(),video.getCreateTime()) > 0 ? true : false;
    }

    //根据id查找视频
    @Override
    public Video queryVideoById(String id) {
        String sql = "SELECT id,user_id,title,url,play_num,likes,create_time FROM tb_video WHERE id=? AND state = 0";
        return JDBCUtil.getInstance(Video.class,sql,id);
    }

    //查询所有视频
    @Override
    public List<Video> queryAllVideo() {
        String sql = "SELECT id,user_id,title,url,play_num,likes,create_time FROM tb_video WHERE state = 0";
        return JDBCUtil.getForList(Video.class,sql);
    }

    @Override
    public List<Video> queryVideoByTitle(String criteria) {
        String param = "%"+criteria+"%";
        String sql = "SELECT id,user_id,title,url,play_num,likes,create_time FROM tb_video WHERE title like ? AND state = 0";
        return JDBCUtil.getForList(Video.class,sql,param);
    }

    @Override
    public boolean deleteVideoById(String id) {
        String sql = "UPDATE tb_video SET state = 1 WHERE id = ? ";
        return JDBCUtil.ALLCUD(sql,id) > 0 ? true : false;
    }

    //查询视频页面视频信息和作者信息列表
    @Override
    public List<VideoSuggestVo> queryVideoInfoList(String id) {
        String sql = "SELECT v.id,v.user_id,v.title,v.url,v.play_num,u.nick_name FROM tb_video v LEFT JOIN tb_user u ON v.user_id = u.id WHERE v.user_id = u.id AND v.id!= ?";
        return JDBCUtil.getForList(VideoSuggestVo.class,sql,id);
    }

    //主页查询视频信息列表
    @Override
    public List<VideoListVo> queryVideoListInfo() {
        String sql = "SELECT v.id,v.title,u.nick_name,v.play_num,v.likes,v.url,v.create_time FROM tb_video v,tb_user u WHERE v.user_id = u.id";
        return JDBCUtil.getForList(VideoListVo.class,sql);
    }



    @Override
    public List<Video> queryVideoByUserId(Long userId) {
        String sql = "SELECT id,user_id,title,url,play_num,create_time,1 as type FROM `tb_video` where user_id = ? and state = 0 ORDER BY create_time DESC";


        List<Video> videos = JDBCUtil.getForList(Video.class, sql, userId);
        return videos ==null || videos.isEmpty() ? null : videos;
    }

    @Override
    public List<Video> SearchByKeyByUserId(String word,Long userId) {
        String param = "%"+word+"%";
        String sql = "SELECT id,user_id,title,url,play_num,create_time,1 as type FROM `tb_video` where title like ? and user_id = ? and state = 0 ORDER BY create_time DESC";
        List<Video> videos = JDBCUtil.getForList(Video.class, sql, param,userId);
        return videos == null || videos.isEmpty() ? null : videos;
    }
}
